package br.com.virtual.replicar.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import br.com.virtual.replicar.model.VirtualModelReplicacao;


public class VirtualModelReplicacaoDAO{

	@PersistenceContext(unitName = "replica_virtual")
	EntityManager replicadorEntityManager;
	

	public boolean create(VirtualModelReplicacao virtualReplicationModel) {

		try{
			
			replicadorEntityManager.merge(virtualReplicationModel);
//			replicadorEntityManager.persist(virtualReplicationModel);
			return true;
			
		}catch(Exception e ){
			e.printStackTrace();e.getCause();
			return false;
		}
	}

	
	public boolean update(VirtualModelReplicacao virtualReplicationModel) {
		
		try {
			replicadorEntityManager.merge(virtualReplicationModel);
			return true;
		} catch (Exception e) {
			e.printStackTrace();e.getCause();
			return false;
		}
		
	}

	public boolean delete(VirtualModelReplicacao virtualReplicationModel, Long id) {

		try {
			
			VirtualModelReplicacao vmr = read(virtualReplicationModel, id);
			
			if( vmr != null )
			replicadorEntityManager.remove( vmr );
			
			return true;
		} catch (Exception e) { 
			e.printStackTrace();e.getCause();
			return false;
		}
	
	}
	
	private VirtualModelReplicacao read(VirtualModelReplicacao virtualModelReplicacao, Long id){
		
		return replicadorEntityManager.find(virtualModelReplicacao.getClass(), id);
	}
	
	
}
